#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
"""
@author:apple
@file:demo03.py
@time:2022/03/11
"""
from browsermobproxy import Server
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common import by

from selenium.webdriver.chrome.options import Options


server = Server(r'./browsermob-proxy-2.1.4/bin/browsermob-proxy')
server.start()
proxy = server.create_proxy()

chrome_options = Options()

chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--proxy-server={0}'.format(proxy.proxy))


web = Chrome(options=chrome_options)
proxy.new_har("baidu", options={'captureHeaders': True, 'captureContent': True})
web.get("https://www.baidu.com")
# web.get("https://cooperation.uniontech.com/")

# //*[@id="kw"]

# web.find_element()
# web.find_element_by_xpath()

time.sleep(2)

result = proxy.har

for entry in result['log']['entries']:
    _url = entry['request']['url']
    # # 根据URL找到数据接口
    if "/sugrec" in _url:
        _response = entry['response']
        _content = _response['content']['text']
        # 获取接口返回内容
        print(_content)

server.stop()
web.quit()

if __name__ == '__main__':
    pass
